This page features a walkthrough of how to build an interactive map based on census data. In this example, I will be using the state of California, my home state, as the base for the map.
The first step in the process, after loading in the packages that I plan to use, is to grab census data. This requires an API key, as shown below.
Your original .Renviron will be backed up and stored in your R HOME directory if needed.
Your API key has been stored in your .Renviron and can be accessed by Sys.getenv("CENSUS_API_KEY").
To use now, restart R or run `readRenviron("~/.Renviron")`
[1] "2a6f8c21a30d3024e038d67d7d4eba647dc79cd4"
After running the code, the API key is stored, and you can comment it so it does not run every time. The next step is to choose the variables you want to examine with your map. This can be done using the code below.
In order to specify that we want to look at California, I have to pull the data and filter for the state. Then, I labeled it for ease of coding in the future.
Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
Next, I am going to simplify the columns for ease of examining the data. The next two bits of code will shave off the MOE Column and remove the ‘e’ from the estimates column to clarify the wording.
Now that the data has been simplified, let’s start by looking at what the map looks like using the ‘median income’ category.
mapview(ca_counties_withgeo, zcol ="medincome")
To change up the color pattern a bit, I’m going to utilize R Color Brewer to shift the palette to one that features yellows and reds to match California’s warmth.
Warning: Found less unique colors (9) than unique zcol values (58)!
Interpolating color vector to match number of zcol values.
The data displayed on the map above show that median income within California counties varies wildly. My home county is 53,350, while San Francisco County has a median income of 126,187. Further analysis of the data to include discrepencies by race, gender, and educational level would provide further insights about the California economical makeup by county.